package com.settlement.system.service.impl;

import com.settlement.system.model.entity.LinkNoHistory;
import com.settlement.system.mapper.LinkNoHistoryMapper;
import com.settlement.system.service.LinkNoHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.LinkNoHistoryQuery;
import com.settlement.system.model.form.LinkNoHistoryForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.LinkNoHistoryImportVo;

/**
 * <p>
 * 单号归档对应关系表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-06-21
 */
@Service
public class LinkNoHistoryServiceImpl extends ServiceImpl<LinkNoHistoryMapper, LinkNoHistory> implements LinkNoHistoryService {

    @Override
    public Page<LinkNoHistory> getPage(LinkNoHistoryQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<LinkNoHistory> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveLinkNoHistory(LinkNoHistoryForm linkNoHistoryForm) {
         LinkNoHistory linkNoHistory = new LinkNoHistory();
         BeanUtils.copyProperties(linkNoHistoryForm, linkNoHistory);
         return this.saveOrUpdate(linkNoHistory);
     }

     @Override
     public LinkNoHistoryForm getLinkNoHistoryFormData(Long id) {
         LinkNoHistory linkNoHistory = this.getById(id);
         LinkNoHistoryForm linkNoHistoryForm = new LinkNoHistoryForm();
         BeanUtils.copyProperties(linkNoHistory, linkNoHistoryForm);
         return linkNoHistoryForm;
     }

     @Override
     public boolean deleteLinkNoHistory(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<LinkNoHistoryImportVo> voList)  {
        List<LinkNoHistory> linkNoHistoryList = new ArrayList<>();
        for (LinkNoHistoryImportVo importVo : voList) {
            LinkNoHistory linkNoHistory = new LinkNoHistory();
            BeanUtils.copyProperties(importVo, linkNoHistory);
            linkNoHistoryList.add(linkNoHistory);
        }
        this.saveOrUpdateBatch(linkNoHistoryList);
    }

    @Override
    public List<LinkNoHistoryImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<LinkNoHistory> linkNoHistoryList = this.getBaseMapper().selectBatchIds(ids);
        List<LinkNoHistoryImportVo> voList = new ArrayList<>();
        for (LinkNoHistory linkNoHistory : linkNoHistoryList) {
            LinkNoHistoryImportVo vo = new LinkNoHistoryImportVo();
            BeanUtils.copyProperties(linkNoHistory, vo);
            voList.add(vo);
        }
        return voList;
    }
}
